package com.example.leetcode.trainingcamp.week7.Thursday;

import java.util.ArrayList;
import java.util.List;

public class Offer34 {

    private List<List<Integer>> list = new ArrayList<>();
    int sum = 0;

    public List<List<Integer>> pathSum(TreeNode root, int target) {
        dfs(root,target,new ArrayList<>());
        return list;
    }

    public void dfs(TreeNode root,int target,List<Integer> t){
        if (root == null){
            return;
        }
        sum += root.val;
        t.add(root.val);
        if (root.left == null && root.right == null){
            if (sum == target){
                List<Integer> t1 = new ArrayList<>(t);
                list.add(t1);
            }
        }
        dfs(root.left,target,t);
        dfs(root.right,target,t);
        sum = sum - t.get(t.size()-1);
        t.remove(t.size() -1);
    }
}
